package com.le.tester.book.effectivejava3rdchinese.articletweeneight;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * createTime：2022/8/17 15:57
 * description：使用泛型声明类别
 */
public class ChooserGeneric<T> {

    //不用数组去声明泛型
    //private final T[] choiceArray;
    private final List<T> choiceArray;

    public ChooserGeneric(Collection<T> choices) {
        choiceArray = new ArrayList<>(choices);
    }

    public T choose() {
        Random random = ThreadLocalRandom.current();

        return choiceArray.get(random.nextInt(choiceArray.size()));
    }
}
